package com.khshop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.khshop.pojo.Loginfo;
import com.khshop.service.LoginfoService;
import com.khshop.vo.AjaxResult;
import com.khshop.vo.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 访问日志 前端控制器
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-16
 */
@Api(tags = "访问日志 前端控制器")
@RestController
@RequestMapping("/")
public class LoginfoController {

    @Autowired
    LoginfoService loginfoService;

    //新增访问日志
    @ApiOperation("新增访问日志")
    @PostMapping("/addLoginfo")
    @ResponseBody
    public AjaxResult addLoginfo(@RequestBody Loginfo loginfo) {
        if (loginfoService.addLogInfo(loginfo).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //修改访问日志
//    @ApiOperation("修改访问日志")
//    @PostMapping("/updateLoginfo")
//    @ResponseBody
//    public AjaxResult updateLoginfo(@RequestBody Loginfo loginfo) {
//        if (loginfoService.updateLogInfo(loginfo).isFlag()) {
//            return AjaxResult.success();
//        } else {
//            return AjaxResult.error();
//        }
//
//    }

    //删除访问日志
    @ApiOperation("删除访问日志")
    @PostMapping("/delLoginfo")
    @ResponseBody
    public AjaxResult delLoginfo(Integer id) {
        if (loginfoService.delLogInfo(id).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //查询访问日志
    @ApiOperation("查询访问日志")
    @PostMapping("/searchLoginfo")
    @ResponseBody
    public AjaxResult searchLoginfo(@RequestBody Map<String, String> map,
                                     @RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "limit", defaultValue = "15") Integer limit) {

        ServiceResult<IPage<Loginfo>> serviceResult = loginfoService.searchLogInfo(map, page, limit);
        if (serviceResult.isFlag()) {
            return AjaxResult.success((int) serviceResult.getData().getTotal(), serviceResult.getData());
        } else {
            return AjaxResult.error();
        }

    }
}

